﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace Web.Admin.Admins
{
    public partial class Pages_Admin_Admins_ManagerUser : NoTheme
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string[] strroles = Roles.GetRolesForUser(User.Identity.Name.ToString());
            string userRoles = "";
            foreach (string item in strroles)
            {
                userRoles += item + ",";
            }
            if (!userRoles.Contains("Administrators"))
            {
                Response.Clear();
                Response.Write("<script language=javascript>window.alert('您没有权限进入本页！\\n请登录或与管理员联系！');history.back();</script>");
                Response.End();
            }

        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            UserViewDiv.Visible = (UserView.SelectedIndex == -1 && UserEditView.CurrentMode != DetailsViewMode.Insert);
        }

        protected void Region1Tabs_MenuItemClick(object sender, MenuEventArgs e)
        {
            if (e.Item.Value == "Users")
                Region1MultiView.SetActiveView(Region1Users);
            else
                Region1MultiView.SetActiveView(Region1Roles);
        }

        protected void BindUserView()
        {
            UserView.SelectedIndex = -1;
            UserView.PageIndex = 0;
            UserView.DataBind();
        }

        protected void UserViewGoButton_Click(object sender, EventArgs e)
        {
            BindUserView();
        }

        protected void UserView_PageIndexChanged(object sender, EventArgs e)
        {
            UserView.SelectedIndex = -1;
        }

        protected void UserEditViewDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.Cancel = String.IsNullOrEmpty(((string)(e.InputParameters["usernameToMatch"])));
        }

        protected void UserEditViewDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.Cancel = true;
            CheckBoxList roles = ((CheckBoxList)(UserEditView.FindControl("UserRolesControl1$CheckBoxList1")));
            MembershipFactory.UpdateUser(e.InputParameters, roles.Items);
            UserView.DataBind();
        }

        protected void UserEditViewDataSource_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.Cancel = true;
            CheckBoxList roles = ((CheckBoxList)(UserEditView.FindControl("UserRolesControl1$CheckBoxList1")));
            MembershipFactory.CreateUser(e.InputParameters, roles.Items);
            BindUserView();
        }

        protected void UserEditViewDataSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            e.Cancel = true;
            MembershipFactory.DeleteUser(((string)(e.InputParameters["UserName"])));
            BindUserView();
        }

        protected void UserEditView_ItemCreated(object sender, EventArgs e)
        {
            ViewHelper.DecorateItem(UserEditView);
        }

        protected void UserEditView_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                UserEditViewMessageBar.Text = e.Exception.ToString(); ;
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
        }

        protected void UserEditView_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                UserEditViewMessageBar.Text = e.Exception.ToString(); ;
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
            }
        }

        protected void UserEditView_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Close")
                BindUserView();
        }

        public void AddRoleButton_Click(object sender, EventArgs e)
        {
            try
            {
                Roles.CreateRole(RoleTextBox.Text);
                UserRolesControl2.PopulateRoles();
                RoleTextBox.Text = String.Empty;
            }
            catch (Exception ex)
            {
                RolesMessageBar.Text = ex.ToString(); ;
            }
        }

        protected void DeleteRolesButton_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (ListItem item in UserRolesControl2.AllRoles)
                {
                    if (item.Selected)
                    {
                        string[] listOfRoles = Roles.GetUsersInRole(item.Value);
                        if (listOfRoles.Length == 0)
                            Roles.DeleteRole(item.Value);
                        else
                            throw new Exception(String.Format("There are {0} user(s) in role \'{1}\'. This role cannot be deleted.", listOfRoles.Length, item.Value));
                    }
                }
            }
            catch (Exception ex)
            {
                RolesMessageBar.Text = ex.ToString(); ;
            }
            UserRolesControl2.PopulateRoles();
        }
    }
}
